package com.lhy.admin.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lhy.admin.common.mvcplus.service.SimpleService;
import com.lhy.admin.mapper.RolePermissionMapper;
import com.lhy.admin.model.Permission;
import com.lhy.admin.model.RolePermission;
import com.lhy.admin.model.extend.RolePermissions;
import com.lhy.admin.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RolePermissionService extends SimpleService<RolePermissionMapper, RolePermission> {

    @Autowired
    PermissionService permissionService;

    @Transactional(rollbackFor = Exception.class)
    public void updateRolePermission(RolePermissions rolePermissions) {
        Integer roleId = rolePermissions.getRoleId();
        List<Integer> newPermissionIds = rolePermissions.getPermissionIds();
        log.debug("将更新权限的角色：{}", roleId);
        log.debug("新permissionIds:{}", CollectionUtil.toString(newPermissionIds));

        //查询原有已存在的permissionIds
        List<RolePermission> oldRolePermissions = this.list(new RolePermission().setRoleId(roleId));
        List<Integer> oldRolePermissionsIds = oldRolePermissions.stream().map(RolePermission::getPermId).collect(Collectors.toList());
        log.debug("已存在的permissionIds:{}", CollectionUtil.toString(oldRolePermissionsIds));

        //rolePermissionsIds
        List<Integer> deleteRolePermissionsIds = oldRolePermissions.stream()
                .filter(item -> !newPermissionIds.contains(item.getPermId()))
                .map(RolePermission::getId)
                .collect(Collectors.toList());
        log.debug("需要删除的permissionIds:{}", CollectionUtil.toString(deleteRolePermissionsIds));
        if (!deleteRolePermissionsIds.isEmpty()) {
            baseMapper.delete(Wrappers.lambdaQuery(RolePermission.class).in(RolePermission::getId,deleteRolePermissionsIds));
        }

        if (!newPermissionIds.isEmpty() ) {
            List<Permission> permissions = permissionService.list(Wrappers.lambdaQuery(Permission.class).in(Permission::getId,newPermissionIds));
            //需要新增的
            List<RolePermission> addRolePermissionIds = newPermissionIds.stream()
                    .filter(item -> !oldRolePermissionsIds.contains(item))
                    .map(aLong -> new RolePermission().setPermId(aLong).setRoleId(roleId).setPerm(getPemissionName(permissions, aLong)))
                    .collect(Collectors.toList());
            log.debug("需要新增的permissionIds:{}", CollectionUtil.toString(addRolePermissionIds));
            addRolePermissionIds.forEach(this::save);
        }

    }

    private String getPemissionName(List<Permission> permissions, Integer permissionId) {
        Optional<String> first = permissions.stream()
                .filter(permission -> permissionId.equals(permission.getId()))
                .map(Permission::getName)
                .findFirst();
        return first.orElse("");
    }

}
